package com.lxl.model;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import com.lxl.main.Game;

/**
 * 我军坦克类
 * @author 刘星鹭
 * @Email liuxinglumail@163.com
 * @version 创建时间： 2014年11月3日 上午10:52:21
 */
public class MyTank {
	/**
	 * 坦克直径
	 */
	public static final int TANK_DIAMETER = 50;
	/**
	 * 坦克颜色
	 */
	public static final Color TANK_COLOR = Color.YELLOW;
	/**
	 * 坦克移动速度
	 */
	public static final int TANK_SPEED = 2;
	/**
	 * 坦克炮筒颜色
	 */
	public static final Color TANK_BARREL_COLOR = Color.BLUE;
	/**
	 * 坦克炮筒长度
	 */
	public static final int TANK_BARREL_LENGTH = TANK_DIAMETER / 2;
	/**
	 * 坦克左上角横坐标
	 */
	private int tankX = Game.FRAME_WIDTH - TANK_DIAMETER;
	/**
	 * 坦克左上角纵坐标
	 */
	private int tankY = Game.FRAME_HEIGHT - TANK_DIAMETER;
	/**
	 * 坦克方向
	 */
	private Direction tankDirection = Direction.STOP;
	/**
	 * 坦克最近一次的方向
	 */
	private Direction tankLastDirection = Direction.UP;
	/**
	 * 坦克最近一次的横坐标
	 */
	private int tankLastX = tankX;
	/**
	 * 坦克最近一次的纵坐标
	 */
	private int tankLastY = tankY;
	/**
	 * 用于记录四个方向键按下与否的flag
	 */
	private boolean tankDirectionKeyPressedFlagUp = false,
			tankDirectionKeyPressedFlagDown = false,
			tankDirectionKeyPressedFlagLeft = false,
			tankDirectionKeyPressedFlagRight = false;
	/**
	 * 记录是否按下空格键的flag
	 */
	private boolean spacePressedFlag = false;
	/**
	 * 记录是否按下Ctrl键的flag
	 */
	private boolean ctrlPressedFlag = false;
	/**
	 * 记录合法时间间隔内是否发射过炮弹的flag
	 */
	private boolean shootedFlag = false;
	/**
	 * 当前子弹
	 */
	private ArrayList<MyBullet> thisBullets = new ArrayList<MyBullet>();
	/**
	 * 当前大子弹
	 */
	private ArrayList<BigBullet> thisBigBullets = new ArrayList<BigBullet>();
	/**
	 * 坦克是否存活
	 */
	private boolean alive = true;

	/**
	 * 画坦克的方法
	 * @param graphics 画笔
	 */
	public void paint(Graphics graphics) {
		// 如果死了就return
		if (!alive)
			return;
		// 移动坦克
		move();
		// 储存当前画笔颜色
		Color backGroundColor = graphics.getColor();
		// 改变当前画笔颜色为坦克颜色
		graphics.setColor(TANK_COLOR);
		// 画出坦克
		graphics.fillOval(tankX, tankY, TANK_DIAMETER, TANK_DIAMETER);
		// 画出炮筒
		paintBarrel(graphics);
		// 如果时间间隔内没发射过子弹，且按下了空格就打出一发子弹
		if (!shootedFlag) {
			if (spacePressedFlag)
				shoot(graphics);
		}
		// 如果时间间隔内没发射过子弹，且按下了Ctrl就打出一发大子弹
		if (!shootedFlag) {
			if (ctrlPressedFlag)
				bigShoot(graphics);
		}
		// 只要有子弹存在就画出所有子弹
		if (thisBullets.size() != 0)
			for (int i = 0; i < thisBullets.size(); i++)
				thisBullets.get(i).paint(graphics);
		// 只要有大子弹存在就画出所有子弹
		if (thisBigBullets.size() != 0)
			for (int i = 0; i < thisBigBullets.size(); i++)
				thisBigBullets.get(i).paint(graphics);

		// 不能使用增强for循环，和ArrayList.remove()方法有冲突
		// for (Bullet bullet : thisBullets)
		// bullet.paint(graphics);

		// 还原当前画笔颜色
		graphics.setColor(backGroundColor);
	}

	/**
	 * 画坦克炮筒方法
	 * @param graphics 画笔
	 */
	private void paintBarrel(Graphics graphics) {
		// 如果死了就return
		if (!alive)
			return;
		// 坦克中心横坐标
		int tankCenterX = tankX + TANK_DIAMETER / 2;
		// 坦克中心纵坐标
		int tankCenterY = tankY + TANK_DIAMETER / 2;
		// 上
		if (tankDirection == Direction.UP) {
			// 储存当前画笔颜色
			Color backGroundColor = graphics.getColor();
			// 改变当前画笔颜色为炮筒颜色
			graphics.setColor(TANK_BARREL_COLOR);
			// 画出炮筒
			graphics.drawLine(tankCenterX, tankCenterY, tankCenterX,
					tankCenterY - TANK_BARREL_LENGTH);
			// 还原双缓冲图片当前画笔颜色
			graphics.setColor(backGroundColor);
		}
		// 下
		else if (tankDirection == Direction.DOWN) {
			// 储存当前画笔颜色
			Color backGroundColor = graphics.getColor();
			// 改变当前画笔颜色为炮筒颜色
			graphics.setColor(TANK_BARREL_COLOR);
			// 画出炮筒
			graphics.drawLine(tankCenterX, tankCenterY, tankCenterX,
					tankCenterY + TANK_BARREL_LENGTH);
			// 还原双缓冲图片当前画笔颜色
			graphics.setColor(backGroundColor);
		}
		// 左
		else if (tankDirection == Direction.LEFT) {
			// 储存当前画笔颜色
			Color backGroundColor = graphics.getColor();
			// 改变当前画笔颜色为炮筒颜色
			graphics.setColor(TANK_BARREL_COLOR);
			// 画出炮筒
			graphics.drawLine(tankCenterX, tankCenterY, tankCenterX
					- TANK_BARREL_LENGTH, tankCenterY);
			// 还原双缓冲图片当前画笔颜色
			graphics.setColor(backGroundColor);
		}
		// 右
		else if (tankDirection == Direction.RIGHT) {
			// 储存当前画笔颜色
			Color backGroundColor = graphics.getColor();
			// 改变当前画笔颜色为炮筒颜色
			graphics.setColor(TANK_BARREL_COLOR);
			// 画出炮筒
			graphics.drawLine(tankCenterX, tankCenterY, tankCenterX
					+ TANK_BARREL_LENGTH, tankCenterY);
			// 还原双缓冲图片当前画笔颜色
			graphics.setColor(backGroundColor);
		}
		// 上左
		else if (tankDirection == Direction.UP_LEFT) {
			// 储存当前画笔颜色
			Color backGroundColor = graphics.getColor();
			// 改变当前画笔颜色为炮筒颜色
			graphics.setColor(TANK_BARREL_COLOR);
			// 画出炮筒
			graphics.drawLine(tankCenterX, tankCenterY,
					(int) (tankCenterX - TANK_BARREL_LENGTH * 0.7),
					(int) (tankCenterY - TANK_BARREL_LENGTH * 0.7));
			// 还原双缓冲图片当前画笔颜色
			graphics.setColor(backGroundColor);
		}
		// 上右
		else if (tankDirection == Direction.UP_RIGHT) {
			// 储存当前画笔颜色
			Color backGroundColor = graphics.getColor();
			// 改变当前画笔颜色为炮筒颜色
			graphics.setColor(TANK_BARREL_COLOR);
			// 画出炮筒
			graphics.drawLine(tankCenterX, tankCenterY,
					(int) (tankCenterX + TANK_BARREL_LENGTH * 0.7),
					(int) (tankCenterY - TANK_BARREL_LENGTH * 0.7));
			// 还原双缓冲图片当前画笔颜色
			graphics.setColor(backGroundColor);
		}
		// 下左
		else if (tankDirection == Direction.DOWN_LEFT) {
			// 储存当前画笔颜色
			Color backGroundColor = graphics.getColor();
			// 改变当前画笔颜色为炮筒颜色
			graphics.setColor(TANK_BARREL_COLOR);
			// 画出炮筒
			graphics.drawLine(tankCenterX, tankCenterY,
					(int) (tankCenterX - TANK_BARREL_LENGTH * 0.7),
					(int) (tankCenterY + TANK_BARREL_LENGTH * 0.7));
			// 还原双缓冲图片当前画笔颜色
			graphics.setColor(backGroundColor);
		}
		// 下右
		else if (tankDirection == Direction.DOWN_RIGHT) {
			// 储存当前画笔颜色
			Color backGroundColor = graphics.getColor();
			// 改变当前画笔颜色为炮筒颜色
			graphics.setColor(TANK_BARREL_COLOR);
			// 画出炮筒
			graphics.drawLine(tankCenterX, tankCenterY,
					(int) (tankCenterX + TANK_BARREL_LENGTH * 0.7),
					(int) (tankCenterY + TANK_BARREL_LENGTH * 0.7));
			// 还原双缓冲图片当前画笔颜色
			graphics.setColor(backGroundColor);
		}
		// 停止
		else if (tankDirection == Direction.STOP) {
			if (tankLastDirection == Direction.UP) {
				// 储存当前画笔颜色
				Color backGroundColor = graphics.getColor();
				// 改变当前画笔颜色为炮筒颜色
				graphics.setColor(TANK_BARREL_COLOR);
				// 画出炮筒
				graphics.drawLine(tankCenterX, tankCenterY, tankCenterX,
						tankCenterY - TANK_BARREL_LENGTH);
				// 还原双缓冲图片当前画笔颜色
				graphics.setColor(backGroundColor);
			}
			// 下
			else if (tankLastDirection == Direction.DOWN) {
				// 储存当前画笔颜色
				Color backGroundColor = graphics.getColor();
				// 改变当前画笔颜色为炮筒颜色
				graphics.setColor(TANK_BARREL_COLOR);
				// 画出炮筒
				graphics.drawLine(tankCenterX, tankCenterY, tankCenterX,
						tankCenterY + TANK_BARREL_LENGTH);
				// 还原双缓冲图片当前画笔颜色
				graphics.setColor(backGroundColor);
			}
			// 左
			else if (tankLastDirection == Direction.LEFT) {
				// 储存当前画笔颜色
				Color backGroundColor = graphics.getColor();
				// 改变当前画笔颜色为炮筒颜色
				graphics.setColor(TANK_BARREL_COLOR);
				// 画出炮筒
				graphics.drawLine(tankCenterX, tankCenterY, tankCenterX
						- TANK_BARREL_LENGTH, tankCenterY);
				// 还原双缓冲图片当前画笔颜色
				graphics.setColor(backGroundColor);
			}
			// 右
			else if (tankLastDirection == Direction.RIGHT) {
				// 储存当前画笔颜色
				Color backGroundColor = graphics.getColor();
				// 改变当前画笔颜色为炮筒颜色
				graphics.setColor(TANK_BARREL_COLOR);
				// 画出炮筒
				graphics.drawLine(tankCenterX, tankCenterY, tankCenterX
						+ TANK_BARREL_LENGTH, tankCenterY);
				// 还原双缓冲图片当前画笔颜色
				graphics.setColor(backGroundColor);
			}
			// 上左
			else if (tankLastDirection == Direction.UP_LEFT) {
				// 储存当前画笔颜色
				Color backGroundColor = graphics.getColor();
				// 改变当前画笔颜色为炮筒颜色
				graphics.setColor(TANK_BARREL_COLOR);
				// 画出炮筒
				graphics.drawLine(tankCenterX, tankCenterY,
						(int) (tankCenterX - TANK_BARREL_LENGTH * 0.7),
						(int) (tankCenterY - TANK_BARREL_LENGTH * 0.7));
				// 还原双缓冲图片当前画笔颜色
				graphics.setColor(backGroundColor);
			}
			// 上右
			else if (tankLastDirection == Direction.UP_RIGHT) {
				// 储存当前画笔颜色
				Color backGroundColor = graphics.getColor();
				// 改变当前画笔颜色为炮筒颜色
				graphics.setColor(TANK_BARREL_COLOR);
				// 画出炮筒
				graphics.drawLine(tankCenterX, tankCenterY,
						(int) (tankCenterX + TANK_BARREL_LENGTH * 0.7),
						(int) (tankCenterY - TANK_BARREL_LENGTH * 0.7));
				// 还原双缓冲图片当前画笔颜色
				graphics.setColor(backGroundColor);
			}
			// 下左
			else if (tankLastDirection == Direction.DOWN_LEFT) {
				// 储存当前画笔颜色
				Color backGroundColor = graphics.getColor();
				// 改变当前画笔颜色为炮筒颜色
				graphics.setColor(TANK_BARREL_COLOR);
				// 画出炮筒
				graphics.drawLine(tankCenterX, tankCenterY,
						(int) (tankCenterX - TANK_BARREL_LENGTH * 0.7),
						(int) (tankCenterY + TANK_BARREL_LENGTH * 0.7));
				// 还原双缓冲图片当前画笔颜色
				graphics.setColor(backGroundColor);
			}
			// 下右
			else if (tankLastDirection == Direction.DOWN_RIGHT) {
				// 储存当前画笔颜色
				Color backGroundColor = graphics.getColor();
				// 改变当前画笔颜色为炮筒颜色
				graphics.setColor(TANK_BARREL_COLOR);
				// 画出炮筒
				graphics.drawLine(tankCenterX, tankCenterY,
						(int) (tankCenterX + TANK_BARREL_LENGTH * 0.7),
						(int) (tankCenterY + TANK_BARREL_LENGTH * 0.7));
				// 还原双缓冲图片当前画笔颜色
				graphics.setColor(backGroundColor);
			}
		}
	}

	/**
	 * 发射大子弹方法
	 * @param graphics 画笔
	 */
	private void bigShoot(Graphics graphics) {
		// 如果死了就return
		if (!alive)
			return;
		// 坦克头横坐标
		int tankHeadX = tankX;
		// 坦克头纵坐标
		int tankHeadY = tankY;
		// 上
		if (tankLastDirection == Direction.UP) {
			tankHeadX = (int) (tankX + TANK_DIAMETER / 2.0 - BigBullet.BULLET_DIAMETER / 2.0);
			tankHeadY = tankY - BigBullet.BULLET_DIAMETER;
		}
		// 下
		else if (tankLastDirection == Direction.DOWN) {
			tankHeadX = (int) (tankX + TANK_DIAMETER / 2.0 - BigBullet.BULLET_DIAMETER / 2.0);
			tankHeadY = tankY + TANK_DIAMETER;
		}
		// 左
		else if (tankLastDirection == Direction.LEFT) {
			tankHeadX = tankX - BigBullet.BULLET_DIAMETER;
			tankHeadY = (int) (tankY + TANK_DIAMETER / 2.0 - BigBullet.BULLET_DIAMETER / 2.0);
		}
		// 右
		else if (tankLastDirection == Direction.RIGHT) {
			tankHeadX = tankX + TANK_DIAMETER;
			tankHeadY = (int) (tankY + TANK_DIAMETER / 2.0 - BigBullet.BULLET_DIAMETER / 2.0);
		}
		// 其他方向不允许发射
		else
			return;
		BigBullet thisBullet = new BigBullet(tankLastDirection, tankHeadX,
				tankHeadY);
		thisBigBullets.add(thisBullet);
		thisBullet.paint(graphics);
		thisBullet.setThisBigBullets(thisBigBullets);
		shootedFlag = true;
	}

	/**
	 * 发射子弹方法
	 * @param graphics 画笔
	 */
	private void shoot(Graphics graphics) {
		// 如果死了就return
		if (!alive)
			return;
		// 坦克头横坐标
		int tankHeadX = tankX;
		// 坦克头纵坐标
		int tankHeadY = tankY;
		// 上
		if (tankLastDirection == Direction.UP) {
			tankHeadX = (int) (tankX + TANK_DIAMETER / 2.0 - MyBullet.BULLET_DIAMETER / 2.0);
			tankHeadY = tankY - MyBullet.BULLET_DIAMETER;
		}
		// 下
		else if (tankLastDirection == Direction.DOWN) {
			tankHeadX = (int) (tankX + TANK_DIAMETER / 2.0 - MyBullet.BULLET_DIAMETER / 2.0);
			tankHeadY = tankY + TANK_DIAMETER;
		}
		// 左
		else if (tankLastDirection == Direction.LEFT) {
			tankHeadX = tankX - MyBullet.BULLET_DIAMETER;
			tankHeadY = (int) (tankY + TANK_DIAMETER / 2.0 - MyBullet.BULLET_DIAMETER / 2.0);
		}
		// 右
		else if (tankLastDirection == Direction.RIGHT) {
			tankHeadX = tankX + TANK_DIAMETER;
			tankHeadY = (int) (tankY + TANK_DIAMETER / 2.0 - MyBullet.BULLET_DIAMETER / 2.0);
		}
		// 上左
		else if (tankLastDirection == Direction.UP_LEFT) {
			tankHeadX = (int) (tankX + TANK_DIAMETER / 2.0 * 0.1);
			tankHeadY = (int) (tankY + TANK_DIAMETER / 2.0 * 0.1);
		}
		// 上右
		else if (tankLastDirection == Direction.UP_RIGHT) {
			tankHeadX = (int) (tankX + TANK_DIAMETER / 2.0 * 0.7 + TANK_DIAMETER / 2.0);
			tankHeadY = (int) (tankY + TANK_DIAMETER / 2.0 * 0.1);
		}
		// 下左
		else if (tankLastDirection == Direction.DOWN_LEFT) {
			tankHeadX = (int) (tankX + TANK_DIAMETER / 2.0 * 0.1);
			tankHeadY = (int) (tankY + TANK_DIAMETER / 2.0 * 0.7 + TANK_DIAMETER / 2.0);
		}
		// 下右
		else if (tankLastDirection == Direction.DOWN_RIGHT) {
			tankHeadX = (int) (tankX + TANK_DIAMETER / 2.0 * 0.7 + TANK_DIAMETER / 2.0);
			tankHeadY = (int) (tankY + TANK_DIAMETER / 2.0 * 0.7 + TANK_DIAMETER / 2.0);
		}
		MyBullet thisBullet = new MyBullet(tankLastDirection, tankHeadX,
				tankHeadY);
		thisBullets.add(thisBullet);
		thisBullet.paint(graphics);
		thisBullet.setThisBullets(thisBullets);
		shootedFlag = true;
	}

	/**
	 * 撞墙处理方法
	 * @param wall 欲判断的墙
	 */
	public void hitWall(Wall wall) {
		if (getSurroundRectangle().intersects(wall.getSurroundRectangle())) {
			// 回到过去
			backToLast();
		}
	}

	/**
	 * 坦克移动方法
	 */
	private void move() {
		// 如果死了就return
		if (!alive)
			return;
		// 将上一个X记录
		tankLastX = tankX;
		// 将上一个Y记录
		tankLastY = tankY;
		// 只有不越界的时候才能移动
		if ((tankX >= 0 && tankX <= Game.FRAME_WIDTH - TANK_DIAMETER)
				&& (tankY >= Game.HEADER_HEIGHT && tankY <= Game.FRAME_HEIGHT
						- TANK_DIAMETER)) {
			// 上
			if (tankDirection == Direction.UP) {
				tankY -= TANK_SPEED;
			}
			// 下
			else if (tankDirection == Direction.DOWN) {
				tankY += TANK_SPEED;
			}
			// 左
			else if (tankDirection == Direction.LEFT) {
				tankX -= TANK_SPEED;
			}
			// 右
			else if (tankDirection == Direction.RIGHT) {
				tankX += TANK_SPEED;
			}
			// 上左
			else if (tankDirection == Direction.UP_LEFT) {
				tankX -= TANK_SPEED * 0.7;
				tankY -= TANK_SPEED * 0.7;
			}
			// 上右
			else if (tankDirection == Direction.UP_RIGHT) {
				tankX += TANK_SPEED * 0.7;
				tankY -= TANK_SPEED * 0.7;
			}
			// 下左
			else if (tankDirection == Direction.DOWN_LEFT) {
				tankX -= TANK_SPEED * 0.7;
				tankY += TANK_SPEED * 0.7;
			}
			// 下右
			else if (tankDirection == Direction.DOWN_RIGHT) {
				tankX += TANK_SPEED * 0.7;
				tankY += TANK_SPEED * 0.7;
			}
		} else {
			if (tankX < 0)
				tankX = 0;
			if (tankX > Game.FRAME_WIDTH - TANK_DIAMETER)
				tankX = Game.FRAME_WIDTH - TANK_DIAMETER;
			if (tankY < Game.HEADER_HEIGHT)
				tankY = Game.HEADER_HEIGHT;
			if (tankY > Game.FRAME_HEIGHT - TANK_DIAMETER)
				tankY = Game.FRAME_HEIGHT - TANK_DIAMETER;
		}
	}

	/**
	 * 判断坦克方向方法
	 */
	private void judgeTankDirection() {
		// 如果死了就return
		if (!alive)
			return;
		// 上
		if (tankDirectionKeyPressedFlagUp && !tankDirectionKeyPressedFlagDown
				&& !tankDirectionKeyPressedFlagLeft
				&& !tankDirectionKeyPressedFlagRight) {
			// 如果是当前方向不是停止，将其记录为上一方向
			if (tankDirection != Direction.STOP)
				tankLastDirection = tankDirection;
			// 根据按键flag改变当前方向
			tankDirection = Direction.UP;
		}
		// 下
		else if (!tankDirectionKeyPressedFlagUp
				&& tankDirectionKeyPressedFlagDown
				&& !tankDirectionKeyPressedFlagLeft
				&& !tankDirectionKeyPressedFlagRight) {
			// 如果是当前方向不是停止，将其记录为上一方向
			if (tankDirection != Direction.STOP)
				tankLastDirection = tankDirection;
			// 根据按键flag改变当前方向
			tankDirection = Direction.DOWN;
		}
		// 左
		else if (!tankDirectionKeyPressedFlagUp
				&& !tankDirectionKeyPressedFlagDown
				&& tankDirectionKeyPressedFlagLeft
				&& !tankDirectionKeyPressedFlagRight) {
			// 如果是当前方向不是停止，将其记录为上一方向
			if (tankDirection != Direction.STOP)
				tankLastDirection = tankDirection;
			// 根据按键flag改变当前方向
			tankDirection = Direction.LEFT;
		}
		// 右
		else if (!tankDirectionKeyPressedFlagUp
				&& !tankDirectionKeyPressedFlagDown
				&& !tankDirectionKeyPressedFlagLeft
				&& tankDirectionKeyPressedFlagRight) {
			// 如果是当前方向不是停止，将其记录为上一方向
			if (tankDirection != Direction.STOP)
				tankLastDirection = tankDirection;
			// 根据按键flag改变当前方向
			tankDirection = Direction.RIGHT;
		}
		// 上左
		else if (tankDirectionKeyPressedFlagUp
				&& !tankDirectionKeyPressedFlagDown
				&& tankDirectionKeyPressedFlagLeft
				&& !tankDirectionKeyPressedFlagRight) {
			// 如果是当前方向不是停止，将其记录为上一方向
			if (tankDirection != Direction.STOP)
				tankLastDirection = tankDirection;
			// 根据按键flag改变当前方向
			tankDirection = Direction.UP_LEFT;
		}
		// 上右
		else if (tankDirectionKeyPressedFlagUp
				&& !tankDirectionKeyPressedFlagDown
				&& !tankDirectionKeyPressedFlagLeft
				&& tankDirectionKeyPressedFlagRight) {
			// 如果是当前方向不是停止，将其记录为上一方向
			if (tankDirection != Direction.STOP)
				tankLastDirection = tankDirection;
			// 根据按键flag改变当前方向
			tankDirection = Direction.UP_RIGHT;
		}
		// 下左
		else if (!tankDirectionKeyPressedFlagUp
				&& tankDirectionKeyPressedFlagDown
				&& tankDirectionKeyPressedFlagLeft
				&& !tankDirectionKeyPressedFlagRight) {
			// 如果是当前方向不是停止，将其记录为上一方向
			if (tankDirection != Direction.STOP)
				tankLastDirection = tankDirection;
			// 根据按键flag改变当前方向
			tankDirection = Direction.DOWN_LEFT;
		}
		// 下右
		else if (!tankDirectionKeyPressedFlagUp
				&& tankDirectionKeyPressedFlagDown
				&& !tankDirectionKeyPressedFlagLeft
				&& tankDirectionKeyPressedFlagRight) {
			// 如果是当前方向不是停止，将其记录为上一方向
			if (tankDirection != Direction.STOP)
				tankLastDirection = tankDirection;
			// 根据按键flag改变当前方向
			tankDirection = Direction.DOWN_RIGHT;
		}
		// 停止
		else if (!tankDirectionKeyPressedFlagUp
				&& !tankDirectionKeyPressedFlagDown
				&& !tankDirectionKeyPressedFlagLeft
				&& !tankDirectionKeyPressedFlagRight) {
			// 如果是当前方向不是停止，将其记录为上一方向
			if (tankDirection != Direction.STOP)
				tankLastDirection = tankDirection;
			// 根据按键flag改变当前方向
			tankDirection = Direction.STOP;
		}
	}

	/**
	 * 回到上一位置方法
	 */
	public void backToLast() {
		tankX = tankLastX;
		tankY = tankLastY;
	}

	/**
	 *  按键处理方法
	 * @param e 按下的键的信息
	 */
	public void keyPressed(KeyEvent e) {
		// 如果死了就return
		if (!alive)
			return;
		// 判断按下的键是什么
		switch (e.getKeyCode()) {
		// 方向判断
			case KeyEvent.VK_LEFT :
				tankDirectionKeyPressedFlagLeft = true;
				break;
			case KeyEvent.VK_UP :
				tankDirectionKeyPressedFlagUp = true;
				break;
			case KeyEvent.VK_DOWN :
				tankDirectionKeyPressedFlagDown = true;
				break;
			case KeyEvent.VK_RIGHT :
				tankDirectionKeyPressedFlagRight = true;
				break;
			// 发射子弹判断
			case KeyEvent.VK_SPACE :
				spacePressedFlag = true;
				break;
			// 发射大子弹判断
			case KeyEvent.VK_CONTROL :
				ctrlPressedFlag = true;
				break;
		}
		// 判断坦克方向
		judgeTankDirection();
	}

	/**
	 * 松键处理方法
	 * @param e 松开的键的信息
	 */
	public void keyReleased(KeyEvent e) {
		// 如果死了就return
		if (!alive)
			return;
		// 判断松开的键是什么
		switch (e.getKeyCode()) {
			case KeyEvent.VK_LEFT :
				tankDirectionKeyPressedFlagLeft = false;
				break;
			case KeyEvent.VK_UP :
				tankDirectionKeyPressedFlagUp = false;
				break;
			case KeyEvent.VK_DOWN :
				tankDirectionKeyPressedFlagDown = false;
				break;
			case KeyEvent.VK_RIGHT :
				tankDirectionKeyPressedFlagRight = false;
				break;
			// 发射子弹判断
			case KeyEvent.VK_SPACE :
				spacePressedFlag = false;
				break;
			// 发射大子弹判断
			case KeyEvent.VK_CONTROL :
				ctrlPressedFlag = false;
				break;
		}
		// 判断坦克方向
		judgeTankDirection();
	}

	/**
	 * 判断子弹是否击中敌人
	 * @param enemyTanks 敌军坦克ArrayList
	 */
	public void hit(ArrayList<EnemyTank> enemyTanks) {
		// 如果死了就return
		if (!alive)
			return;
		// 如果是自己撞到敌人
		for (int i = 0; i < enemyTanks.size(); i++) {
			if (getSurroundRectangle().intersects(
					enemyTanks.get(i).getSurroundRectangle())) {
				backToLast();
				enemyTanks.get(i).backToLast();
			}
		}
		// 如果是子弹撞到敌人
		for (int i = 0; i < thisBullets.size(); i++) {
			// 调用当前弹夹的hit方法，判断是否击中
			thisBullets.get(i).hit(enemyTanks);

		}
		// 看是否是大子弹击中敌人
		for (int i = 0; i < thisBigBullets.size(); i++) {
			// 调用当前弹夹的hit方法，判断是否击中
			thisBigBullets.get(i).hit(enemyTanks);

		}
	}

	/**
	 * 获得围绕当前圆形的矩形
	 * @return 围绕当前圆形的矩形
	 */
	public Rectangle getSurroundRectangle() {
		return new Rectangle(tankX, tankY, TANK_DIAMETER, TANK_DIAMETER);
	}

	/**
	 * shootedFlag的setter
	 * @param shootedFlag 允许间隔内是否射击过
	 */
	public void setShootedFlag(boolean shootedFlag) {
		this.shootedFlag = shootedFlag;
	}
	/**
	 * alive的setter
	 * @param alive 是否存活
	 */
	public void setAlive(boolean alive) {
		this.alive = alive;
	}
	/**
	 * alive的getter
	 * @return true为存活
	 */
	public boolean isAlive() {
		return alive;
	}

	/**
	 * thisBullets的getter
	 * @return 当前弹夹
	 */
	public ArrayList<MyBullet> getThisBullets() {
		return thisBullets;
	}
	/**
	 * thisBigBullets的getter
	 * @return 当前大弹夹
	 */
	public ArrayList<BigBullet> getThisBigBullets() {
		return thisBigBullets;
	}
}
